<?php
/**
 * Wufoo_Form
 *
 * @category   Wufoo
 * @package    Wufoo_Form
 * @copyright  Copyright (c) 2009-2010 Eyeflow Internet Marketing. (http://www.eyeflow.com)
 * @author     Sergei Izvorean
 */
class Wufoo_Form_Element
{
    /**
     * Initialize and return most appropriate zend form element
     * to a certain Wufoo element type.
     *
     * @param string $name Element name
     * @param string $wufooType Element type
     * @return Zend_Form_Element
     */
    public static function getElementInstance($name, $wufooType)
    {
        $element = null;

        switch ($wufooType) {
            case 'address':
                switch ($name) {
                    case 'country':
                        $element = new Wufoo_Form_Element_Country($name);
                        break;
                    case 'state':
                        $element = new Wufoo_Form_Element_State($name);
                        break;
                    default:
                        $element = new Zend_Form_Element_Text($name);
                        break;
                }
                break;
            case 'date':
                $element = new Wufoo_Form_Element_Date($name);
                break;
            case 'textarea':
                $element = new Zend_Form_Element_Textarea($name);
                break;
            case 'select':
                $element = new Zend_Form_Element_Select($name);
                break;
            case 'radio':
                $element = new Zend_Form_Element_Radio($name);
                break;
            case 'checkbox':
                $element = new Zend_Form_Element_MultiCheckbox($name);
                break;
            default:
                $element = new Zend_Form_Element_Text($name);
                break;
        }

        return $element;
    }

    /**
     * Convert Wufoo field to a Zend_Form_Element object
     *
     * @param $field object Field defenition from Wufoo Api query
     * @return Zend_Form_Element|Zend_Form_DisplayGroup
     */
    public static function factory($field)
    {
        $filter = Wufoo_Form::getNameFilter();

        /**
         * Filter to php-friendly name
         *
         * @var $element Zend_Form_Element
         */
        $elementName = $filter->filter($field->Title);
        $element = self::getElementInstance($elementName, $field->Typeof);
        switch ($field->Typeof) {
            case 'address':
                switch ($elementName) {
                    case 'addressline2':
                        $field->IsRequired = 0;
                        break;
                }
                break;
            case 'url':
                $element->addValidator(new Wufoo_Validate_Uri());
                break;
            case 'email':
                $element->addValidator(new Zend_Validate_EmailAddress());
                break;
            case 'checkbox':
                // TODO: A little inconsistent (Why not have the choices in 'Choices' member rather than in 'SubFields'?)
                $options = array();
                $field->DefaultVal = array();
                foreach ($field->SubFields as $choice) {
                    $options[$choice->ColumnId] = $choice->ChoicesText; // TODO: Is ColumnId really an ApiId?

                    if ($choice->DefaultVal == 1) {
                        $field->DefaultVal[] = $choice->ColumnId;
                    }
                }

                $element->setMultiOptions($options);
                break;
            case 'date':
                // TODO: Move validation rules to {@see Wufoo_Form_Element_Date}
                $element->addValidator(new Zend_Validate_Date('MM/DD/YYYY'));
                break;
            case 'radio':
            case 'select':
                // TODO: Add input box to 'Other' option
                $options = array();
                foreach ($field->Choices as $choice) {
                    $options[$choice->ChoiceId] = $choice->Choice;
                    
                    if ($choice->IsDefault == 1) {
                        $field->DefaultVal = $choice->ChoiceId;
                    }
                }

                $element->setMultiOptions($options);
                break;
            case 'number':
                if ($field->RangeMin != 0 && $field->RangeMax != 0) {
                    $element->addValidator(
                        new Zend_Validate_Between($field->RangeMin, $field->RangeMax));
                } elseif ($field->RangeMin != 0) {
                    $element->addValidator(
                        new Zend_Validate_GreaterThan($field->RangeMin));
                } elseif ($field->RangeMax != 0) {
                    $element->addValidator(
                        new Zend_Validate_LessThan($field->RangeMin));
                }
                break;
            case 'phone':
                $element->addFilter(new Wufoo_Filter_PhoneNumber());
                break;
            default:
                break;
        }

        // Set common options
        $element->setLabel($field->Title);
        $element->apiId = $field->ColumnId; // TODO: Does ColumnId always refers to ApiId?

        $class = $element->getAttrib('class');
        $element->setAttrib('class', empty($class) ? $field->Size : $class.' '.$field->Size);

        // TODO: Elements get messed up if position is being set
        /*if ($field->Pos !== "0") {
            $element->setOrder($field->Pos);
        }*/
        if (strlen($field->ClassNames) > 0) {
            $element->setAttrib('class', $field->ClassNames);
        }
        if (!empty($field->DefaultVal)) { // can be an array for MultiCheckbox
            $element->setValue($field->DefaultVal);
        }
        if ($field->IsRequired == 1) {
            $element->setRequired(true);
        }

        return $element;
    }
}